package com.six.apidemic.activity;

import static com.six.apidemic.fragment.main.MainFragment.CAMERA_REQ_CODE;
import static com.six.apidemic.fragment.main.MainFragment.DECODE;
import static com.six.apidemic.httpapi.Constract.SUCC_CODE;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;

import com.lzy.okgo.callback.StringCallback;
import com.six.apidemic.R;
import com.six.apidemic.bean.LoginCallbackBean;
import com.six.apidemic.databinding.ActivityLoginBinding;
import com.six.apidemic.httpapi.HTTPAPI;
import com.six.apidemic.util.GsonUtils;
import com.six.apidemic.util.LogUtils;
import com.six.apidemic.util.SpUtils;

import okhttp3.Call;
import okhttp3.Response;

/**
 * @author Administrator
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

   ActivityLoginBinding binding;
   private Button btnModule;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      binding = DataBindingUtil.setContentView(this,R.layout.activity_login);
      binding.setActivity(this);
      initView();
      initData();
   }

   protected void initView() {
      btnModule = findViewById(R.id.btnLogin);
      btnModule.setOnClickListener(this);
      binding.tvModuleRegister.setOnClickListener(this);
   }

   protected void initData() {
      if (!TextUtils.isEmpty((String) SpUtils.get("module_is_login",""))) {
         toMain();
      }

      requestPermission(CAMERA_REQ_CODE, DECODE);
   }

   @Override
   public void onClick(View view) {
      int id = view.getId();
      if (id == R.id.btnLogin) {
         String name = binding.edtUsername.getText().toString().trim();
         if (TextUtils.isEmpty(name)) {
            showToast("请输入名称");
            return;
         }

         String pwd = binding.etModulePwd.getText().toString().trim();
         if (TextUtils.isEmpty(pwd)) {
            showToast("请输入密码");
            return;
         }

         toLogin(name,pwd);
      }else if (id == R.id.tvModuleRegister){
         startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
      }
   }

   /**
    * 登录
    * @param name
    * @param pwd
    */
   public void toLogin(String name,String pwd){
      HTTPAPI.getInstance().toLogin(name,pwd,new StringCallback(){
         @Override
         public void onSuccess(String s, Call call, Response response) {
            LogUtils.e("登录成功-->"+ s);
            LoginCallbackBean bean = GsonUtils.GsonToBean(s,LoginCallbackBean.class);
            if (bean.getCode() == SUCC_CODE) {
               SpUtils.putBean(LoginActivity.this,"module_user_bean",bean.getResult().getUser());
               SpUtils.put("module_token",bean.getResult().getToken());
               SpUtils.put("module_is_login","1");
               toMain();
            }
         }

         @Override
         public void onError(Call call, Response response, Exception e) {
            LogUtils.e("登录失败-->"+ e.getMessage());
         }
      });
   }

   public void toMain(){
      startActivity(new Intent(LoginActivity.this,MainActivity.class));
      finish();
   }

   //编辑请求权限
   private void requestPermission(int requestCode, int mode) {
      ActivityCompat.requestPermissions(
              this,
              new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE},
              requestCode);
   }

   //权限申请返回
   @SuppressLint("MissingSuperCall")
   @Override
   public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
      if (permissions == null || grantResults == null) {
         return;
      }
      if (grantResults.length < 2 || grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
         return;
      }
   }
}
